//
//  TodayViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/12.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "TodayViewController.h"
#import "TodayTableViewCell.h"
#import "TimeView.h"
#import "TAMacro.h"
#import "UIView+Frame.h"
#import "SelfSizeTool.h"
#import "CDMNetTool.h"
#import "UIImageView+WebCache.h"
// model
#import "TodayRootClass.h"
#import "TodayData.h"
#import "TodayList.h"
#import "FooterTableViewCell.h"
#import "DetailsViewController.h"


#define kTableViewCellReuse @"reuse"
#define kTableViewTimeCellReuse @"timeReuse"
#define kRootModelForRow self.rootModel.data[indexPath.section].list[indexPath.row]
#define kRootListCount self.rootModel.data[indexPath.section].list
@interface TodayViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *todayTableView;
@property (strong, nonatomic) TimeView *timeView;
@property (nonatomic, assign)  NSInteger moreRows;
@property (nonatomic, strong) TodayRootClass *rootModel;
@property (nonatomic, assign) BOOL isShow;

@end

@implementation TodayViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.todayTableView registerNib:[UINib nibWithNibName:@"TodayTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellReuse];
    [self.todayTableView registerNib:[UINib nibWithNibName:@"FooterTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewTimeCellReuse];
    
    _timeView = [[TimeView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH / 6.0, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_timeView];
    _todayTableView.separatorStyle = UITableViewCellEditingStyleNone;
    _todayTableView.bounces = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(handleLeft:)];
    _isShow = NO;
    [self handleData];
}

#pragma mark - 实现左按钮点击
- (void)handleLeft:(UIBarButtonItem *)barBtn {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - handleData
- (void)handleData {
    NSString *httpStr = @"http://wanapi.damai.cn/today.json?platform=4&cateid=0&channel_from=m360_market&source=10345&cityid=110000&version=203004";
    [CDMNetTool GET:httpStr andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
//        NSLog(@"请求成功:%@", result);
        self.rootModel = [TodayRootClass modelWithDic:result];
        [self.todayTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败:%@", error);
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.rootModel.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.rootModel.data[section].list.count > 3) {
//        NSLog(@"%ld", _moreRows);
        return 3 + _moreRows + 1;
    } else {
        return self.rootModel.data[section].list.count;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (self.rootModel.data[indexPath.section].list.count <= 3) {
        TodayTableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:kTableViewCellReuse];
        
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.rootModel.data[indexPath.section].list[indexPath.row].pic] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
        // 处理价格
        cell.pricestrLabel.text = kRootModelForRow.pricestr;
        // 处理打折价格
        cell.discountLabel.text = kRootModelForRow.discount;
        
        // 标题赋值
        cell.titleLabel.text = kRootModelForRow.title;
        // 拼接字符串
        NSString *timeStr = kRootModelForRow.timestr;
        NSString *venuename = kRootModelForRow.venuename;
        cell.timstrAndVenuename.text = [timeStr stringByAppendingFormat:@"·%@", venuename];
        [cell.categoryNameButton setTitle:kRootModelForRow.categoryname forState:UIControlStateNormal];
        if (kRootModelForRow.sourceid == 1) {
            [cell.sourceButton setTitle:@"售票" forState:UIControlStateNormal];
        } else if (kRootModelForRow.sourceid == 2) {
            [cell.sourceButton setTitle:@"未知" forState:UIControlStateNormal];
        } else if (kRootModelForRow.sourceid == 3) {
            [cell.sourceButton setTitle:@"报名" forState:UIControlStateNormal];
        }
        
        
        return cell;

    } else {
        if (_isShow) {
            if (indexPath.row == kRootListCount.count) {
                FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewTimeCellReuse];
                [cell packButton:^(UIButton *pack) {
                    _moreRows = 0;
                    _isShow = NO;
                    NSString *title = @"";
                    if (self.rootModel.data[indexPath.section].date < 10) {
                        title = [NSString stringWithFormat:@"0%ld:00", self.rootModel.data[indexPath.section].date];
                    } else {
                        title = [NSString stringWithFormat:@"%ld:00", self.rootModel.data[indexPath.section].date];
                    }
                    
                    [cell.footerButton setTitle:[NSString stringWithFormat:@"查看%@更多项目>>", title] forState:UIControlStateNormal];
                    
                    [self.todayTableView reloadData];
                }];
                
                return cell;
                
            } else {
                TodayTableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:kTableViewCellReuse];
                
                [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.rootModel.data[indexPath.section].list[indexPath.row].pic] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                // 处理价格
                cell.pricestrLabel.text = kRootModelForRow.pricestr;
                // 处理打折价格
                cell.discountLabel.text = kRootModelForRow.discount;
                
                // 标题赋值
                cell.titleLabel.text = kRootModelForRow.title;
                // 拼接字符串
                NSString *timeStr = kRootModelForRow.timestr;
                NSString *venuename = kRootModelForRow.venuename;
                cell.timstrAndVenuename.text = [timeStr stringByAppendingFormat:@"·%@", venuename];
                [cell.categoryNameButton setTitle:kRootModelForRow.categoryname forState:UIControlStateNormal];
                if (kRootModelForRow.sourceid == 1) {
                    [cell.sourceButton setTitle:@"售票" forState:UIControlStateNormal];
                } else if (kRootModelForRow.sourceid == 2) {
                    [cell.sourceButton setTitle:@"未知" forState:UIControlStateNormal];
                } else if (kRootModelForRow.sourceid == 3) {
                    [cell.sourceButton setTitle:@"报名" forState:UIControlStateNormal];
                }
                
                
                return cell;
                
            }

        } else {
            if (indexPath.row == 3) {
                FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewTimeCellReuse];
                [cell showButton:^(UIButton *show) {
                    _moreRows = kRootListCount.count - 3;
                    _isShow = YES;
                    NSString *title = @"";
                    if (self.rootModel.data[indexPath.section].date < 10) {
                        title = [NSString stringWithFormat:@"0%ld:00", self.rootModel.data[indexPath.section].date];
                    } else {
                        title = [NSString stringWithFormat:@"%ld:00", self.rootModel.data[indexPath.section].date];
                    }
                    
                    [cell.footerButton setTitle:[NSString stringWithFormat:@"回收%@更多项目>>", title] forState:UIControlStateNormal];
                    [self.todayTableView reloadData];

                }];
                
            return cell;
                
            } else {
                TodayTableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:kTableViewCellReuse];
                
                [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.rootModel.data[indexPath.section].list[indexPath.row].pic] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
                // 处理价格
                cell.pricestrLabel.text = kRootModelForRow.pricestr;
                // 处理打折价格
                cell.discountLabel.text = kRootModelForRow.discount;
                
                // 标题赋值
                cell.titleLabel.text = kRootModelForRow.title;
                // 拼接字符串
                NSString *timeStr = kRootModelForRow.timestr;
                NSString *venuename = kRootModelForRow.venuename;
                cell.timstrAndVenuename.text = [timeStr stringByAppendingFormat:@"·%@", venuename];
                [cell.categoryNameButton setTitle:kRootModelForRow.categoryname forState:UIControlStateNormal];
                if (kRootModelForRow.sourceid == 1) {
                    [cell.sourceButton setTitle:@"售票" forState:UIControlStateNormal];
                } else if (kRootModelForRow.sourceid == 2) {
                    [cell.sourceButton setTitle:@"未知" forState:UIControlStateNormal];
                } else if (kRootModelForRow.sourceid == 3) {
                    [cell.sourceButton setTitle:@"报名" forState:UIControlStateNormal];
                }
                
                
                return cell;
                
            }

        }
    }
   

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (self.rootModel.data[indexPath.section].list.count > 3) {
            if (_isShow) {
                if (indexPath.row == kRootListCount.count) {
                    return 44;
                } else {
                    CGFloat addHeight = [SelfSizeTool selfSizeHeightWithString:self.rootModel.data[indexPath.section].list[2].title width:(SCREEN_WIDTH * 5 / 6.0 - 15) font:[UIFont systemFontOfSize:16]];
                    return 250 + 0.5 + addHeight;
 
                }
            } else {
                if (indexPath.row == 3) {
                    return 44;
                } else {
                    CGFloat addHeight = [SelfSizeTool selfSizeHeightWithString:self.rootModel.data[indexPath.section].list[indexPath.row].title width:(SCREEN_WIDTH * 5 / 6.0 - 15) font:[UIFont systemFontOfSize:16]];
                    return 250 + 0.5 + addHeight;

                }
            }
        } else {
            CGFloat addHeight = [SelfSizeTool selfSizeHeightWithString:self.rootModel.data[indexPath.section].list[indexPath.row].title width:(SCREEN_WIDTH * 5 / 6.0 - 15) font:[UIFont systemFontOfSize:16]];
            return 250  + 0.5 + addHeight;
        }
 
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.rootModel.data[section].date < 10) {
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 5 / 6.0, 44)];
        headView.backgroundColor = [UIColor colorWithWhite:0.961 alpha:1.000];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 5 / 6.0, 44)];
        [headView addSubview:label];
        
        label.text = [NSString stringWithFormat:@"0%ld:00", self.rootModel.data[section].date];
        label.textAlignment = NSTextAlignmentCenter;
        return headView;
    } else  {
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 5 / 6.0, 44)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 5 / 6.0, 44)];
        [headView addSubview:label];
        label.text = [NSString stringWithFormat:@"%ld:00", self.rootModel.data[section].date];
        label.textAlignment = NSTextAlignmentCenter;
        headView.backgroundColor = [UIColor colorWithWhite:0.961 alpha:1.000];
        return headView;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
  
    detailsVC.parm = [NSString stringWithFormat:@"%@", self.rootModel.data[indexPath.section].list[indexPath.row].ID];
    UINavigationController *detaNavi = [[UINavigationController alloc] initWithRootViewController:detailsVC];
    
    detaNavi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:detaNavi animated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
